const jwt=require('jsonwebtoken')
const tokenSecret='ziji'
module.exports=(req,res,next)=>{
    let token=req.headers.token||req.headers.Token||req.headers.authentication||req.headers.Authentication
    if(!token){
        res.send({code:401,msg:'token不能为空'})
        return
    }
    try{
        let payload=jwt.verify(token,tokenSecret)
    if(Date.now()>payload.expired){
        res.send({code:402,msg:'token已经过期'})
        return
    }else{
        req.token=payload
        next()
    }

    }catch(err){
        res.send({code:403,msg:'token不合法'})
    }
}